﻿using MDIDemo.PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy
{
    public partial class Form_Registry : DevExpress.XtraEditors.XtraForm
    {
        public Form_Registry(string Mac)
        {
            iniForm(Mac, false, new DateTime(2020, 5, 2));
        }
        public Form_Registry(string Mac, DateTime date)
        {
            iniForm(Mac, true, date);
        }
        private string myMac;
        private bool okSign;
        private DateTime registryDate;
        private string RegistryCode;

        private void iniForm(string Mac, bool okSign, DateTime date)
        {
            InitializeComponent();
            this.myMac = Mac;
            this.okSign = okSign;
            this.registryDate = date;
            textEdit1.Text = Mac;
            string message = "温馨提示：";
            if (okSign)
            {
                message += string.Format("注册限期为{0}!", date.ToString("yyyy年MM月dd日"));
                label3.ForeColor = Color.Green;
            }
            else
            {
                message += "请尽快注册！";
                label3.ForeColor = Color.Red;
            }
            label3.Text = message;
            textEdit2.Focus();
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        public string GetRegistryCode()
        {
            return RegistryCode;
        }
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (textEdit2.Text == null || textEdit2.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入注册码！", "温馨提示"
                , MessageBoxButtons.OK
                , MessageBoxIcon.Error
                , MessageBoxDefaultButton.Button1);
                return;
            }
            if (this.myMac != null && this.myMac.Length > 0)
            {
                string registryCodeKey = "asdfasr2";
                Class_EncryptDes class_EncryptDes = new Class_EncryptDes();
                RegistryCode = textEdit2.Text.Trim();
                string RegistryCodeDec = class_EncryptDes.DesDecrypt(RegistryCode, registryCodeKey);
                DateTime dateTime;
                bool validDate = DateTime.TryParse(RegistryCodeDec, out dateTime);
                if (validDate)
                {
                    validDate = dateTime >= DateTime.Now;
                }
                if (!validDate)
                {
                    MessageBox.Show("注册码无效！", "温馨提示"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Error
                    , MessageBoxDefaultButton.Button1);
                }
                else
                    this.DialogResult = DialogResult.OK;
            }
        }
    }
}
